package com.crm.service.impl.messageManager;

import com.crm.dao.mapper.MessageManager.MessageMapper;
import com.crm.dao.mapper.customMapper.messageManagerCustomMapper.MessageCustomMapper;
import com.crm.model.entity.MessageManager.Message;
import com.crm.model.entity.MessageManager.MessageExample;
import com.crm.model.qo.messageManager.MessageQO;
import com.crm.model.vo.messageManager.MessageVO;
import com.crm.model.vo.messageManager.MsgVO;
import com.crm.service.messageManager.MessageDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 信息
 */
@Service
public class MessageDbServiceImpl implements MessageDbService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageCustomMapper messageCustomMapper;

    /**
     * 信息
     * @param messageQO
     * @return
     */
    @Override
    public List<MessageVO> selectMessageList(MessageQO messageQO) {
        return messageCustomMapper.selectMessageList(messageQO);
    }

    /**
     * 修改消息
     * @param message
     * @return
     */
    @Override
    public Boolean updateMessage(Message message) {
        Boolean result = false;
        int num = messageMapper.updateByPrimaryKeySelective(message);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 新增消息
     * @param message
     * @return
     */
    @Override
    public Boolean addMessage(Message message) {
        Boolean result = false;
        int num = messageMapper.insertSelective(message);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过id查询消息
     * @param messageId 消息id
     * @return
     */
    @Override
    public MsgVO selectById(String messageId) {
        return messageCustomMapper.selectById(messageId);
    }

    /**
     *通过主题id查询消息
     * @param themeId
     * @return
     */
    @Override
    public List<MsgVO> selectByThemeId(String themeId) {
        List<MsgVO> list = messageCustomMapper.selectByThemeId(themeId);
        return list;
    }

    /**
     * 通过主题id和接收人id查询
     * @param themeId
     * @return
     */
    @Override
    public List<Message> selectBySenderIdAndReceiverId(String themeId) {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andThemeIdEqualTo(themeId);
        criteria.andPMessageIdIsNotNull();
        criteria.andDeleteStatusEqualTo(0);
        List<Message> list = messageMapper.selectByExample(messageExample);
        return list;
    }

    /**
     * 删除消息
     * @param message
     * @return
     */
    @Override
    public Boolean updateMsg(Message message) {
        return messageCustomMapper.updateMsg(message);
    }

    /**
     * 通过主题id和pid的为空的查询
     * @param themeId
     * @return
     */
    @Override
    public List<Message> selectThemeId(String themeId) {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andThemeIdEqualTo(themeId);
        criteria.andDeleteStatusEqualTo(0);
        List<Message> list = messageMapper.selectByExample(messageExample);
        return list;
    }

    /**
     * 通过消息id查询
     * @param messageId
     * @return
     */
    @Override
    public Message selectByMessageId(String messageId) {
        return messageMapper.selectByPrimaryKey(messageId);
    }
}
